package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.Global;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.CachedCommandUtil;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_CollectDeviceServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_CollectDevice_ValveStatusServiceHelper;
import com.hzhr.iot.collect.util.DateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.collectData.CollectDataModel;
import com.hzhr.iot.device.sdk.model.collectData.CollectDataPostModel;
import com.hzhr.iot.device.sdk.model.collectDevice.CollectDeviceModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.service.Device_CollectDataService;
import com.hzhr.iot.device.sdk.service.Device_CollectDeviceService;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * NB-IoT燃气表累积用量查询
 * @author 48310
 *
 */
@Component
public class MessageProcessor20 implements MessageProcessor {
	@Autowired
	Device_CollectDataService device_collectDataService;
	@Autowired
	Device_IotDeviceService device_iotDeviceService;
	@Autowired
	Device_CollectDeviceService device_collectDeviceService;
	@Autowired
	Global global;
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	CachedCommandUtil cachedCommandUtil;
	@Autowired
	Device_CollectDeviceServiceHelper device_collectDeviceServiceHelper;
	@Autowired
	Device_CollectDevice_ValveStatusServiceHelper device_collectDevice_valveStatusServiceHelper;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		//查询物联网设备
		IotDeviceModel iotDeviceModel = null;
		String iotDeviceCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID));
		if (!StringUtils.isEmpty(iotDeviceCode)) {
			Map<String, String> header = new HashMap<>();
			header.put("token", global.getFirstToken());
			Map<String, Object> query = new HashMap<>();
			query.put("_show", "one");
			query.put("_equal_iotPlatformDeviceId", deviceId);
			iotDeviceModel = device_iotDeviceService.get(header, query).getModel();
		}
		if (iotDeviceModel == null) throw new Exception("物联网设备不存在");

		//查询采集设备
		Map<String, String> header = new HashMap<>();
		header.put("token", global.getFirstToken());
		Map<String, Object> query = new HashMap<>();
		query.put("_show", "one");
		query.put("_equal_iotDeviceId", iotDeviceModel.getId());
		CollectDeviceModel collectDeviceModel = device_collectDeviceService.get(header, query).getModel();

		//新增采集数据
		if (parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_DATE) != null && parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_TIME) != null) {
			Date currentTime = DateUtil.strToDate(parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_DATE).toString() +
					parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_TIME).toString(), "yyyyMMddHHmmss");

			Map<String, String> header1 = new HashMap<>();
			header1.put("token", global.getFirstToken());
			Map<String, Object> query1 = new HashMap<>();
			query1.put("_show", "one");
			query1.put("_equal_iotDeviceId", iotDeviceModel.getId());
			query1.put("_equal_currentTm", currentTime.getTime());
			CollectDataModel existCollectData = device_collectDataService.get(header1, query1).getModel();

			if (existCollectData == null) {
				CollectDataPostModel postModel = new CollectDataPostModel();
				if (collectDeviceModel != null) {
					postModel.setCollectDeviceId(collectDeviceModel.getId());
				}
				postModel.setIotDeviceId(iotDeviceModel.getId());

				if (parsedMessageObj.get(MessageFieldKeyEnum.FREEZING_DATE) != null && parsedMessageObj.get(MessageFieldKeyEnum.FREEZING_TIME) != null) {
					Date freezingTime = DateUtil.strToDate(parsedMessageObj.get(MessageFieldKeyEnum.FREEZING_DATE).toString() +
							parsedMessageObj.get(MessageFieldKeyEnum.FREEZING_TIME).toString(), "yyyyMMddHHmmss");
					postModel.setFreezingTime(freezingTime.getTime());
				}
				postModel.setFreezingValue(HzhrTypeUtil.object2double(parsedMessageObj.get(MessageFieldKeyEnum.FREEZING_VALUE)));

				String valveStatus = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RESP_VALVE_STATUS));
				if ("0".equals(valveStatus)) {
					postModel.setValveStatusCode("2");
					if (collectDeviceModel != null) {
						device_collectDevice_valveStatusServiceHelper.putByCollectDeviceId(collectDeviceModel.getId(), "2");
					}
				} else if ("1".equals(valveStatus)) {
					postModel.setValveStatusCode("1");
					if (collectDeviceModel != null) {
						device_collectDevice_valveStatusServiceHelper.putByCollectDeviceId(collectDeviceModel.getId(), "1");
					}
				} else if ("2".equals(valveStatus)) {
					postModel.setValveStatusCode("3");
					if (collectDeviceModel != null) {
						device_collectDevice_valveStatusServiceHelper.putByCollectDeviceId(collectDeviceModel.getId(), "3");
					}
				}

				postModel.setCurrentTm(currentTime.getTime());
				postModel.setCurrentValue(HzhrTypeUtil.object2double(parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_VALUE)));
				postModel.setRsrq(HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RSRQ)));
				postModel.setRssi(HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.RSSI)));
				postModel.setSnr(HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.SNR)));
				postModel.setCellId(HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.CELLID)));
				postModel.setPci(HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.PCI)));
				postModel.setEarfcn(HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.EARFCN)));
				query = new HashMap<>();
				device_collectDataService.post(header, query, postModel);
			}
		}

		//清除指令缓存
		cachedCommandUtil.deleteCachedCommand(deviceId,
				parsedMessageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID).toString(),
				parsedMessageObj.get(MessageFieldKeyEnum.MESSAGE_ID).toString());
		return null;
	}
}
